<?php

namespace iBrand\Shop\Backend\Providers;

use iBrand\Component\Goods\Repositories\GoodsRepository;
use iBrand\Shop\Backend\Console\PurchaseBackendMenusCommand;
use iBrand\Shop\Backend\Console\ShopBackendMenusCommand;
use iBrand\Shop\Backend\Console\ShopCityCommand;
use iBrand\Shop\Backend\Repository\Eloquent\GoodsRepositoryEloquent;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Menu;
use Event;
use iBrand\Shop\Backend\ShopBackend;

class BackendServiceProvider extends ServiceProvider
{
	/**
	 * This namespace is applied to your controller routes.
	 *
	 * In addition, it is set as the URL generator's root namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'iBrand\Shop\Backend\Http\Controllers';

	protected $subscribe = [
		'iBrand\Shop\Backend\Listeners\onCreateClerkListener',
	];

	/**
	 * Define your route model bindings, pattern filters, etc.
	 *
	 * @return void
	 */
	public function boot()
	{
		if (!$this->app->routesAreCached()) {
			$this->mapWebRoutes();
		}

		ShopBackend::boot();

		$this->loadViewsFrom(__DIR__ . '/../../resources/views', 'shop-backend');

		$this->loadMigrationsFrom(__DIR__ . '/../../migrations');

		//添加配置文件
		$this->publishes([
			__DIR__ . '/../../config/city.php' => config_path('city.php'),
		], 'shop-city-config');

		$this->publishes([
			__DIR__ . '/../../resources/assets' => public_path('assets/backend/shop'),
		], 'shop-backend-assets');

		$this->publishes([
			__DIR__ . '/../../resources/assets/js'  => public_path('assets/backend/admin/js'),
			__DIR__ . '/../../resources/assets/css' => public_path('assets/backend/admin/css'),
		]);

		$this->publishes([
			__DIR__ . '/../../resources/assets/backend' => public_path('assets/backend/libs'),
		]);

		$this->publishes([
			__DIR__ . '/../../resources/assets/img' => public_path('assets/backend/admin/img'),
		]);

		$this->registerMenu();

		foreach ($this->subscribe as $subscriber) {
			Event::subscribe($subscriber);
		}

		$this->commands([
			ShopBackendMenusCommand::class,
			PurchaseBackendMenusCommand::class,
			ShopCityCommand::class,
		]);
	}

	public function register()
	{
		$this->app->bind(GoodsRepository::class, GoodsRepositoryEloquent::class);
	}

	/**
	 * Define the "web" routes for the application.
	 *
	 * These routes all receive session state, CSRF protection, etc.
	 *
	 * @return void
	 */
	protected function mapWebRoutes()
	{
		Route::group([
			'middleware' => ['web', 'admin'],
			'namespace'  => $this->namespace,
		], function ($router) {
			require __DIR__ . '/../Http/routes.php';
		});
	}

	private function registerMenu()
	{
		Menu::make('topMenu', function ($menu) {

			$menu->add('<i class="iconfont icon-mendianguanli"></i>
                            <span>门店管理</span>', ['url' => 'admin/shop/list/store', 'secure' => env('SECURE')])->active('admin/shop/*');
		});
	}
}
